package io.renren.modules.classinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.classinfo.dao.ClassStuRealationDao;
import io.renren.modules.classinfo.vo.ClassStuRealationVO;
import io.renren.modules.classinfo.dto.ClassStuRealationDTO;
import io.renren.modules.classinfo.entity.ClassStuRealationEntity;
import io.renren.modules.classinfo.service.ClassStuRealationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 学生班级关系管理
 *
 * @author test test
 * @since 1.0.0 2024-09-13
 */
@Service
public class ClassStuRealationServiceImpl extends CrudServiceImpl<ClassStuRealationDao, ClassStuRealationEntity, ClassStuRealationVO> implements ClassStuRealationService {

    @Override
    public QueryWrapper<ClassStuRealationEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<ClassStuRealationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void save(ClassStuRealationDTO classStuRealationDTO) {
        ClassStuRealationEntity classStuRealationEntity = ConvertUtils.sourceToTarget(classStuRealationDTO, ClassStuRealationEntity.class);
        this.insert(classStuRealationEntity);
    }

    @Override
    public void update(ClassStuRealationDTO classStuRealationDTO) {
        ClassStuRealationEntity classStuRealationEntity = ConvertUtils.sourceToTarget(classStuRealationDTO, ClassStuRealationEntity.class);
        this.updateById(classStuRealationEntity);
    }

    @Override
    public void deleteByStudentId(Long studentId) {
        QueryWrapper<ClassStuRealationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        this.baseDao.delete(wrapper);
    }

    @Override
    public List<ClassStuRealationVO> queryByClassInfoId(Long classInfoId) {
        QueryWrapper<ClassStuRealationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("class_info_id", classInfoId);
        List<ClassStuRealationEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, ClassStuRealationVO.class);
    }

    @Override
    public ClassStuRealationVO queryByStudentId(Long studentId) {
        QueryWrapper<ClassStuRealationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        ClassStuRealationEntity classStuRealationEntity = this.baseDao.selectOne(wrapper);
        return ConvertUtils.sourceToTarget(classStuRealationEntity, ClassStuRealationVO.class);
    }


}